package demo28;

/**
 * 缺点：锁住了一个方法，锁的力度有点大
 */
public class Singleton2 {
    private static Singleton2 instance;
    private Singleton2(){

    }
    public static synchronized Singleton2 getInstance(){    //对获取实例的方法进行同步
        if (instance == null)
            instance = new Singleton2();
        return instance;
    }

    public static void main(String[] args) {
        Runnable runnable = () -> System.out.println(Singleton2.getInstance());
        new Thread(runnable,"t1").start();
        new Thread(runnable,"t2").start();
    }
}